package com.gk.panda.apis.controller;

import com.gk.panda.apis.dto.EquipmentPointDto;
import com.gk.panda.apis.entity.TransectManager.dto.GetEquipmentPointDto;
import com.gk.panda.apis.entity.TransectManager.vo.EquipmentPointVo;
import com.gk.panda.apis.log.Log;
import com.gk.panda.apis.service.EquipmentPointService;
import com.gk.panda.commons.controller.BaseController;
import com.gk.panda.commons.exception.ServerException;
import com.gk.panda.commons.page.TableDataInfo;
import com.gk.panda.commons.result.Result;
import com.gk.panda.commons.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;

/**
 * @ClassName: EquipmentPointController
 * @Description: 设备点位
 * @author: Sir.yang
 * @date: 2022/11/9 18:37
 * @version: 1.0
 **/
@RestController
@Api(tags = "设备点位")
@RequestMapping("/ePoint")
public class EquipmentPointController extends BaseController {

    @Autowired
    private EquipmentPointService equipmentPointService;

    @ApiOperation(value = "存储设备点位")
    @Log
    @PostMapping("/addep")
    public Result<String> addep(@RequestBody EquipmentPointDto equipmentPointDto) throws UnsupportedEncodingException {
        equipmentPointDto.setCreateBy(UserUtil.getCurrentUserName());

        int result = equipmentPointService.insertequipmentPoint(equipmentPointDto);
        if(result >0){
            return  Result.write(200,"设备点位存储成功");
        }
        return Result.error(201,"设备点位存储失败");
    }

    @ApiOperation(value = "修改设备点位")
    @Log
    @PostMapping("/updateEP")
    public Result<String> updateEP(@RequestBody EquipmentPointDto equipmentPointDto) throws UnsupportedEncodingException {
        equipmentPointDto.setUpdateBy(UserUtil.getCurrentUserName());

        int result = equipmentPointService.updateEquipmentPoint(equipmentPointDto);
        if(result >0){
            return  Result.write(200,"设备点位存储成功");
        }
        return Result.error(201,"设备点位存储失败");
    }

    @ApiOperation("PC查看设备点位详情")
    @GetMapping("/getInfo/{id}")
    public Result<EquipmentPointVo> getTransectDetails(@PathVariable Integer id) {
        if(null == id){
            throw new ServerException("设备点位id不能为空");
        }
        return Result.ok(equipmentPointService.getEPInfo(id));
    }

    @ApiOperation("查看设备点位列表")
    @PostMapping("/getEPList")
    public Result<TableDataInfo> getTransList(@Validated @RequestBody GetEquipmentPointDto equipmentPointDto) {
        startPage();
        return Result.ok(getDataTable(equipmentPointService.getEPList(equipmentPointDto)));
    }

    @ApiOperation("删除设备点位")
    @Log
    @DeleteMapping("/remove/{EPId}")
    public Result<Integer> removeTransect(@PathVariable Integer EPId) {
        return Result.ok(equipmentPointService.removeEPById(EPId));
    }

    @ApiOperation("设备点位同步设备表数据")
    @Log
    @GetMapping("/syncDevicePoint")
    public Result<String> syncDevicePoint() throws ParseException {
        equipmentPointService.syncDevicePoint();
        return Result.ok("同步成功");
    }
}
